<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
          <el-form-item label="轮播图或视频地址" prop="bannerUrl">
        <el-input v-model="dataForm.bannerUrl" placeholder="轮播图或视频地址"></el-input>
      </el-form-item>
          <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
          <el-form-item label="类型 " prop="type">
        <el-input v-model="dataForm.type" placeholder="类型 "></el-input>
      </el-form-item>
          <el-form-item label="字典 海报标签 banner_tag 风格" prop="bannerTag">
        <el-input v-model="dataForm.bannerTag" placeholder="字典 海报标签 banner_tag 风格"></el-input>
      </el-form-item>
          <el-form-item label="工作的轮播图" prop="jobId">
        <el-input v-model="dataForm.jobId" placeholder="工作的轮播图"></el-input>
      </el-form-item>
          <el-form-item label="链接地址" prop="linkUrl">
        <el-input v-model="dataForm.linkUrl" placeholder="链接地址"></el-input>
      </el-form-item>
          <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
          <el-form-item label="内容  海报则替换内容生成" prop="content">
        <el-input v-model="dataForm.content" placeholder="内容  海报则替换内容生成"></el-input>
      </el-form-item>
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '',  bannerUrl: '',  sort: '',  type: '',  bannerTag: '',  jobId: '',  linkUrl: '',  title: '',  content: ''});

const rules = ref({
          bannerUrl: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          sort: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          type: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          bannerTag: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          jobId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          linkUrl: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          title: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          content: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
  });

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/api/banner/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/api/banner", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
